package com.org.khtn.command;

import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.org.khtn.command.delete.DeleteQuery;
import com.org.khtn.command.insert.InsertQuery;
import com.org.khtn.command.select.SelectQuery;
import com.org.khtn.command.update.UpdateQuery;
import com.org.khtn.stringcommand.Format;
import com.org.khtn.ultilities.Ultilities;

// nhan 1 dong query thi loc roi forward den lop xu ly phu hop
// tra ve -1 neu khong ton tai

public class Command {
	String queryString;
	String databaseName;
	final String className = getClass().getSimpleName();

	public Command(String command, String DatabaseName) {
		queryString = command;
		databaseName = DatabaseName;
		QueryPattern.createPattern();
	}

	public String[] listCommand = new String[] { "SELECT", "DELETE", "INSERT",
			"UPDATE" };

	// tra ve key phu hop voi tung command
	// 0 select
	// 1 Delete
	// 2 Insert
	// 3 Update

	public int getForwardKey(String queryString) {
		Format formatString = new Format(queryString);
		String temp = formatString.XLC();
		temp = temp.toUpperCase();
		int length = listCommand.length;
		for (int i = 0; i < length; i++) {

			if (temp.contains(listCommand[i])) {
				return i;
			}
		}
		return -1;
	}

	public Vector execute() {
		final String methodName = "execute():";

		String myPattern = "((" + QueryPattern.DELETE_PATTERN + ")|("
				+ QueryPattern.SELECT_PATTERN + ")|("
				+ QueryPattern.INSERT_PATTERN + ")|("
				+ QueryPattern.UPDATE_PATTERN + "))";
		String myPattern12 = "(" + QueryPattern.SELECT_PATTERN + "|"
				+ QueryPattern.DELETE_PATTERN + "|"
				+ QueryPattern.INSERT_PATTERN + "|"
				+ QueryPattern.UPDATE_PATTERN + ")";
		String pattern = myPattern + "(\\s+" + QueryPattern.SELECT_PATTERN
				+ ")*" + "(\\s+" + QueryPattern.INSERT_PATTERN + ")*" + "(\\s+"
				+ QueryPattern.DELETE_PATTERN + ")*" + "(\\s+"
				+ QueryPattern.UPDATE_PATTERN + ")*";
		Pattern p11 = Pattern.compile(pattern);
		Matcher m11 = p11.matcher(queryString);
		Vector result = new Vector<>();

		if (!m11.matches()) {
			result.add(-10);
			return result;
		}
		System.out.println(className + methodName + "queryString:"
				+ queryString);
		Pattern p = Pattern.compile(myPattern);
		Matcher m = p.matcher(queryString);
		while (m.find()) {
			System.out.println("Masking: " + m.group());
			Object ob = executeAQuery(m.group());
			System.out.println(ob);
			result.add(ob);
		}
		return result;
	}

	public Object executeAQuery(String queryString) {
		final String methodName = "executeAQuery():";
		int key = getForwardKey(queryString);
		System.out.println(className + methodName + "key:" + key);
		MyQuery query;

		switch (key) {

		case 0:
			System.out.println(className + methodName + "1");
			SelectQuery mp = new SelectQuery(queryString, databaseName);
			return mp.executeReader();
		case 1:
			System.out.println(className + methodName + "2");
			query = new DeleteQuery(queryString, databaseName);
			return query.executeNonQuery();

		case 2:
			query = new InsertQuery(queryString, databaseName);
			return query.executeNonQuery();
		case 3:
			query = new UpdateQuery(queryString, databaseName);
			return query.executeNonQuery();

		}
		return null;
	}

	public static void main(String[] args) {
		Command cmd = new Command(
				"select * from dns where Son='saa'"
						, "test");
		System.out.println(cmd.execute());
	}

}